package com.ys.controller.api.outer;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bo.util.Base64Util;
import com.bo.util.HttpUtil;
import com.ys.config.MyConfig;
import com.ys.vo.R;

@CrossOrigin
@Controller
@RequestMapping("/api/outer/upload")
public class ApiOuterUploadController {

	@RequestMapping("/uploadfileForEditor")
	@ResponseBody
	public void uploadfileForEditor(Model model, HttpServletRequest request, HttpServletResponse response,
			@RequestParam("fileStream") MultipartFile file) throws FileUploadException, IOException {

		PrintWriter out = response.getWriter();

		try {
			InputStream fileInputStream = file.getInputStream();
			
			// 文件初始名
			String oriFilename = file.getOriginalFilename();
			
			// 将文件保存到本地
			byte[] buffer = new byte[1024 * 1024];
			int byteread = 0;
			while ((byteread = fileInputStream.read(buffer)) != -1) {
			}
			fileInputStream.close();

		    String b = Base64Util.encode(buffer);
		    System.out.println(b);
		    

			
			// 图片请求请求地址
			String url = MyConfig.getUrlFile() + "/api/outer/upload/uploadBase64";
			
			JSONObject paramJO = new JSONObject();
			paramJO.put("base64Data", b);
			paramJO.put("moduleFile", "nomodule");
			paramJO.put("sourceFileName", oriFilename);
			/*
			 * {"msg":"操作成功","code":0,"data":{"url":"https://file.mini-ease.com/upload/2023/09/27/213128649.jpg"}}
			 */
			String result = HttpUtil.sendPostJSON(url, paramJO);
			System.out.println(result);
			JSONObject resultJO = JSON.parseObject(result);

			JSONObject obj = new JSONObject();
			obj.put("error", 0);
			obj.put("url", resultJO.getJSONObject("data").getString("url"));
			out.println(obj.toJSONString());
			try {
				return;
			} catch (Exception e) {
				out.println(getError("上传失败"));
				return;
			}
		} catch (IOException e) {
			e.printStackTrace();
			out.println(getError("上传失败"));
		}
	}

	private String getError(String message) {
		JSONObject obj = new JSONObject();
		obj.put("error", 1);
		obj.put("message", message);
		return obj.toJSONString();
	}
    
    /**
     * 上传文件（给angular使用）
     * @param uploadfile
     * @param moduleFile 文件模块
     */
	@PostMapping("/uploadForAngularFileUpload")
	@ResponseBody
	public R uploadForAngularFileUpload(@RequestParam("file") MultipartFile uploadfile, String moduleFile) throws Exception{
		
		
        return R.ok();
    }
}
